package com.bawei.user


import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.mvvm.BaseActivity
import com.bawei.user.databinding.ActivityUserCheckBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.*
import com.bawei.base.bean.AddUserEntity

@AndroidEntryPoint
@Route(path = "/user/UserCheckActivity")
class UserCheckActivity : BaseActivity<ActivityUserCheckBinding, UserViewModel>() {
    lateinit var timer: Timer
    lateinit var addUserEntity: AddUserEntity
    override fun initView() {
        binding.layCon.setOnClickListener {
            ARouter.getInstance().build("/user/MapActivity").navigation()
        }
        timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                val currentTime =
                    SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(System.currentTimeMillis())
                val split = currentTime.split(" ")
                runOnUiThread {
                    binding.checkData.text = split[0]
                    binding.checkTime.text = split[1]
                }
            }
        }, 0, 1000)
        if (SPUtils.getInstance().getString("startTime").isNotEmpty()){
            binding.checkStartandEndTime.text=SPUtils.getInstance().getString("startTime")+"-"
        }
        if (SPUtils.getInstance().getString("endTime").isNotEmpty()){
            binding.checkStartandEndTime.text= binding.checkStartandEndTime.text.toString()+SPUtils.getInstance().getString("endTime")
        }

        binding.check.setOnClickListener {
            if (binding.checkStartandEndTime.text.toString().isNotEmpty()){
                val split = binding.checkStartandEndTime.text.toString().split("-")
                if (split[1].isNotEmpty()){
                    ToastUtils.showLong("今日打卡已完毕")
                    return@setOnClickListener
                }
            }

            var currentTime =
                SimpleDateFormat("HH:mm").format(System.currentTimeMillis())
            if (binding.checkStartandEndTime.text.isEmpty()) {
                binding.checkStartandEndTime.text=currentTime+"-"
                SPUtils.getInstance().put("startTime",currentTime)
            } else {
                SPUtils.getInstance().put("endTime",currentTime)
                binding.checkStartandEndTime.text= binding.checkStartandEndTime.text.toString()+currentTime
                addUserEntity= AddUserEntity()
                var currentData =
                    SimpleDateFormat("yyyy年MM月dd日").format(System.currentTimeMillis())
                val split = binding.checkStartandEndTime.text.toString().split("-")
                addUserEntity.apply {
                    morningCheckIn=currentData+split[0]
                    afternoonCheckIn=currentData+split[1]
                    currentLocation="天津八维"
                    status = if (split[0]<="09:00"&&split[1]>="18:00"){
                        1
                    }else if (split[0]>="09:00"&&split[1]>="18:00"){
                        2
                    }else if (split[0]<="09:00"&&split[1]<="18:00"){
                        3
                    }else{
                        4
                    }
                    userId=9
                    user=9
                    location="教室"
                }
                viewModel.addUser(addUserEntity)
            }

        }
    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }
}